package com.hxh.components
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.filters.DropShadowFilter;
	
	import mx.events.FlexEvent;
	import mx.preloaders.DownloadProgressBar;

	public class PreloaderFLEX extends DownloadProgressBar
	{
		private var _myLoader:MyLoader;
		
		public function PreloaderFLEX(title:String="LOADING...")
		{
			_myLoader = new MyLoader();
			_myLoader.Title.text = title;
			_myLoader.filters = [new DropShadowFilter(2, 45, 0, 0.5)];
			addChild(_myLoader);
			addEventListener(Event.ADDED_TO_STAGE, onAdded);
		}
		
		public function setTitle(title:String):void{
			_myLoader.Title.text = title;
		}
		
		public override function set preloader(preloader:Sprite):void{
			preloader.addEventListener(ProgressEvent.PROGRESS, onProgress);
			preloader.addEventListener(FlexEvent.INIT_COMPLETE, initComplete);
		}
		
		private function onProgress(evt:ProgressEvent):void{
			var percent:Number = Math.ceil((evt.bytesLoaded / evt.bytesTotal)* 100);
			_myLoader.Percent.text = percent.toString()+ "%";
			_myLoader.LoadingBar.width = _myLoader.BAR_WIDTH * percent / 100;
		}
		
		private function initComplete(evt:Event):void{
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private function onAdded(evt:Event):void{
			_myLoader.x = 120;
			_myLoader.y = 50;
		}
	}
}